<?php

namespace app\models;

use app\models\Fiction;
use yii\db\ActiveRecord;
use Yii;

class Fiction extends ActiveRecord{
	
	
	public static function tableName(){
		// return "shop_admin";
		return "{{%Fiction}}";
	}

	public function rules(){
        return [
            [['fId'], 'required'],
        ];
    }

    public function attributeLabels(){
        return [
            'fId' => '小说id',
            'fTid' => '小说类型id',
            'fName' => '小说名称',
            'fAuthor' => '小说作者',
            'fCover' => '小说封面',
            'fCollection' => '小说收藏量',
            'fReadcount' => '小说阅读量',
            'fNowupdate' => '小说最新一集',
            'fSaveurl' => '储存路径',
            'fUpdatetime' => '最新更新时间',
            'state' => '状态',
        ];
    }
    
    //插入数据
    public function insertFiction($data){
        //load是php框架内部自主加载数据的函数，而validate是自主验证rules
    }

    //查找所有小说
    public function getAll(){
    	$data = self::find()->select(['fId','fTid','fName','fAuthor','fCover'])->where('state = :state',[':state'=>'1'])->asArray()->all();
		return $data;
    }

    //根据fId获取某一小说全部信息
    public function getByfId($fId){
        $data = self::find()->select(['fId','fName','fAuthor','fCover','fCollection','fReadcount','fSaveurl','fNowupdate','fUpdatetime'])->where('fId = :fId and state = :state',[':fId'=>$fId,':state'=>'1'])->asArray()->one();
        return $data;
    }

    //根据fId获取某一漫画的名字和封面
    public function getRecordInfoByfId($fId){
        $data = self::find()->select(['fId','fName','fCover','fSaveurl'])->where('fId = :fId and state = :state',[':fId'=>$fId,':state'=>'1'])->asArray()->one();
        return $data;
    }

    //根据cid获取某一漫画的名字和封面和储存路径
    public function getInfoByfId($fId){
        $data = self::find()->select(['fId','fName','fCover','fNowupdate'])->where('fId = :fId and state = :state',[':fId'=>$fId,':state'=>'1'])->asArray()->one();
        return $data;
    }

    //查找所有类型名称
    public function getBytype($fTid){
    	$data = self::find()->select(['fId','fName','fAuthor','fCover'])->where('fTid = :fTid and state = :state',[':fTid'=>$fTid,':state'=>'1'])->orderBy('fUpdatetime desc')->asArray()->all();
		return $data;
    }

    //获取收藏量排行榜
    public function getBycollection(){
    	$data = self::find()->select(['fId','fName','fCover','fCollection','fNowupdate','fUpdatetime'])->where('fCollection > :fCollection and state = :state',[':fCollection'=>'0',':state'=>'1'])->orderBy('fCollection desc')->asArray()->all();
		return $data;
    }

    //获取阅读量排行榜
    public function getByreadcount(){
    	$data = self::find()->select(['fId','fName','fCover','fReadcount','fNowupdate','fUpdatetime'])->where('fReadcount > :fReadcount and state = :state',['fReadcount'=>'0',':state'=>'1'])->orderBy('fReadcount desc')->asArray()->all();
		return $data;
    }

    //获取更新时间最新的小说
    public function getByUpdatetopten(){
        $data = self::find()->select(['fId','fName','fCover','fNowupdate', 'fUpdatetime'])->where('state = :state',[':state'=>'1'])->orderBy('fUpdatetime desc')->asArray()->all();
        return $data;
    }

    //获取更新时间最新的小说
    public function getByUpdatetopthree(){
        $data = self::find()->select(['fId','fName','fCover'])->where('state = :state',[':state'=>'1'])->orderBy('fUpdatetime desc')->limit(3)->asArray()->all();
        return $data;
    }

    //查找对应名字的小说
    public function getSearch($expression){
        if(!empty($expression)){
            //User::findBySql('SELECT * FROM user')->all();  此方法是用 sql  语句查询 user 表里面的所有数据； 
            $sql = 'SELECT fId,fTid,fName,fAuthor,fCover FROM Fiction WHERE fName REGEXP '.'"'.$expression.'"';
            $result = Fiction::findBySql($sql)->all();
            if(!empty($result)){
                return $result;
            }  
        }
        return false;
    }

    //更新某漫画收藏量
    public function updateCollection($fId){
        $fiction_info = Fiction::getByfId($fId);
        $fCollection = $fiction_info['fCollection'];
        $key = [
            'fId' => $fId,
        ];
        $changedata = [
            'fCollection' => $fCollection+1,
        ];
        $result = Fiction::updateAll($changedata,$key);
        if( $result == 1 ){
            return true;
        }
        return false;
    }

    //取消收藏的收藏量
    public function updateUnCollection($fId){
        $fiction_info = Fiction::getByfId($fId);
        $fCollection = $fiction_info['fCollection'];
        $key = [
            'fId' => $fId,
        ];
        $changedata = [
            'fCollection' => $fCollection-1,
        ];
        $result = Fiction::updateAll($changedata,$key);
        if( $result == 1 ){
            return true;
        }
        return false;
    }

    //阅读量更新
    public function updateReadcount($fId){
        $fiction_info = Fiction::getByfId($fId);
        $fReadcount = $fiction_info['fReadcount'];
        $key = [
            'fId' => $fId,
        ];
        $changedata = [
            'fReadcount' => $fReadcount+1,
        ];
        $result = Fiction::updateAll($changedata,$key);
        if( $result == 1 ){
            return true;
        }
        return false;
    }
}
